using System.Data;
using System.Data.SqlClient;

namespace VMukti.DataAccess
{
    public class ClsPageDataService : ClsDataServiceBase
    {
        public ClsPageDataService() : base() { }

        public ClsPageDataService(IDbTransaction txn) : base(txn) { }

        public DataSet GetPages()
        {
            return ExecuteDataSet("select pagetitle,id from page where isdeleted='false'", CommandType.Text, null);
        }

        public int AddPage(int intPageId, string strPageTitle, string strPageDesc, bool blnIsPublic, int intUserId)
        {
            SqlCommand cmd;
            int id;
            ExecuteNonQuery(out cmd, "spAEPage",
                CreateParameter("@pID", SqlDbType.BigInt, intPageId),
                CreateParameter("@pPageTitle", SqlDbType.NVarChar, strPageTitle),
                CreateParameter("@pDescription", SqlDbType.NVarChar, strPageDesc),
                CreateParameter("@pIsPublic", SqlDbType.Bit, blnIsPublic),
                CreateParameter("@pUserID", SqlDbType.BigInt, intUserId),
                CreateParameter("@pReturnMaxID", SqlDbType.BigInt, ParameterDirection.Output));

            id = int.Parse(cmd.Parameters["@pReturnMaxID"].Value.ToString());
            cmd.Dispose();
            return id;
        }

        public void PageAllocated(int intPageAllocationId, int intPageId, int intUserId)
        {
            ExecuteDataSet("spAEPageAllocation", CommandType.StoredProcedure,
                CreateParameter("@pID", SqlDbType.BigInt, intPageAllocationId),
                CreateParameter("@pPageID", SqlDbType.BigInt, intPageId),
                CreateParameter("@pUserID", SqlDbType.BigInt, intUserId));
        }

        public int GetMaxPageId()
        {
            SqlCommand cmd;
            int id;
            ExecuteNonQuery(out cmd, "spGMaxPageId",
                CreateParameter("@pReturnMaxID", SqlDbType.BigInt, ParameterDirection.Output));
            id = int.Parse(cmd.Parameters["@pReturnMaxID"].Value.ToString());
            cmd.Dispose();
            return id;
        }

        public DataSet GetPageInfo(int intPageId)
        {
            return ExecuteDataSet("select id,pagetitle from page where ID=" + intPageId + "and isdeleted='false'", CommandType.Text, null);
        }

        public DataSet GetPageAllocated(int intUserId)
        {
            return ExecuteDataSet("spGetUsersPageAllocated", CommandType.StoredProcedure,
               CreateParameter("@pUserID", SqlDbType.BigInt, intUserId));

        }

        public void RemovePage(int intPageId)
        {
            ExecuteDataSet("spDPage", CommandType.StoredProcedure, CreateParameter("@pID", SqlDbType.BigInt, intPageId));
        }

         public void RemovePageAllocation(int intPageId,int intUserId)
        {
            ExecuteDataSet("spDPageAllocation", CommandType.StoredProcedure,
                CreateParameter("@pID", SqlDbType.BigInt, intPageId),
                 CreateParameter("@uID", SqlDbType.BigInt, intUserId));
         }

    }
}